//==================================================================
//  Copyright (C) 2006  Davide Pasca
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//==================================================================
///
///
///
///
///
//==================================================================

#include "stdafx.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "rendm5d_texture.h"
#include "rendm5d_mate.h"

//===============================================================================
using namespace PMath;

//===============================================================================
RendM5D::Material::Material( M5D::Material *mmatep, RendDevice *rdevicep ) :
	_mmatep(mmatep),
	_rdevicep(rdevicep)
{
	_mmatep->_userdatap = (void *)this;
}

//===============================================================================
RendM5D::Material::~Material()
{
	// really not much to free here.. texture is done by itself
}

//===============================================================================
void RendM5D::Material::SelectMaterial()
{
	if ( _mmatep->GetBasicBlend() == PSYSGFX::BASICBLEND_OPAQUE )
	{
		_rdevicep->_DrawState.SetBlending( false );
		_rdevicep->_DrawState.SetAlphaTest( false );
	}
	else
	{
		switch ( _mmatep->GetBasicBlend() )
		{
		case PSYSGFX::BASICBLEND_TRANSPARENT:
			_rdevicep->_DrawState.SetAlphaTest(	true, PSYSGFX::TESTFUNC_GREATER, 0.0f );//0.1f );
			_rdevicep->_DrawState.SetBlending(	true,
											PSYSGFX::BLENDFCT_SRC_ALPHA,
											PSYSGFX::BLENDFCT_ONE_MINUS_SRC_ALPHA );
			break;

		case PSYSGFX::BASICBLEND_ADDITIVE:
			_rdevicep->_DrawState.SetAlphaTest( false );
			_rdevicep->_DrawState.SetBlending(	true,
											PSYSGFX::BLENDFCT_ONE,
											PSYSGFX::BLENDFCT_ONE );
			break;

		case PSYSGFX::BASICBLEND_SUBTRACTIVE:
			_rdevicep->_DrawState.SetAlphaTest( false );
			_rdevicep->_DrawState.SetBlending(	true,
											PSYSGFX::BLENDFCT_ZERO,
											PSYSGFX::BLENDFCT_ONE_MINUS_SRC_COLOR );
			break;
		}
	}

	_rdevicep->_DrawState.SetZWrite( _mmatep->IsWriteDepth() );

	if ( _mmatep->_texturep )
	{
		M5D::Texture		*mtexp = _mmatep->_texturep;

		RendM5D::Texture	*rtexp = (RendM5D::Texture *)_mmatep->_texturep->_userdatap;
		rtexp->SelectTexture();
		_rdevicep->_ShaderMng._Std.SetDiffuseTexture( rtexp );
	}
	else
	{
		_rdevicep->_DrawState.SetTexture( NULL );
		_rdevicep->_ShaderMng._Std.SetDiffuseTexture( NULL );
	}

	_rdevicep->_ShaderMng._Std.SetBaseColor( _mmatep->_diffuse,
											  _mmatep->_specular, _mmatep->_shininess,
											  Vector4( _mmatep->_emissive, 0 ) );
}
